package com.expdatacloud.controller.report;

import com.expdatacloud.dto.ApiResponse;
import com.expdatacloud.dto.BadSkuOverThreeDaysDto;
import com.expdatacloud.dto.OverdueMaterialDto;
import com.expdatacloud.dto.UnQualifiedQualityDto;
import com.expdatacloud.entity.ModelsMgt;
import com.expdatacloud.service.IqcDetectionHistoryService;
import com.expdatacloud.service.ModelsMgtService;
import com.expdatacloud.service.ReportsService;
import com.expdatacloud.utils.SaveWhseIdToCookie;
import com.expdatacloud.vo.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;
import java.util.Map;


/**
 * @author jie
 * @date 2019/12/1 -14:10
 */
@RequestMapping("/report")
@Controller
public class ReportController {

    @Autowired
    private ReportsService reportsService;
    @Autowired
    private ModelsMgtService modelsMgtService;
    @Autowired
    private IqcDetectionHistoryService iqcDetectionHistoryService;

    @GetMapping("/getTime")
    @ResponseBody
    public int getTime(HttpServletRequest request) {
        return request.getServletContext().getAttribute("time") == null ? 3000 : Integer.parseInt(request.getServletContext().getAttribute("time").toString());
    }

    /**
     * 调用这个方法可以配置滚动时间
     */
    @GetMapping("/setTime/{time}")
    @ResponseBody
    public void setTime(HttpServletRequest request, @PathVariable String time) {
        request.getServletContext().setAttribute("time", Integer.parseInt(time) * 1000);
    }

    /**
     * 收货预定看板
     */
    @GetMapping("/receivingBoardList")
    @ResponseBody
    public ApiResponse receivingBoardList(HttpSession session) {
        PageInfo<ReceivingBoardVo> pageInfo = new PageInfo<>(reportsService.receivingBoardList());
        session.setAttribute("receivingBoardList", pageInfo.getList());
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 质检合格未上架看板
     */
    @GetMapping("/unQualifiedQualityList")
    @ResponseBody
    public ApiResponse unQualifiedQualityList(HttpSession session,UnQualifiedQualityDto unQualifiedQualityDto) {
        PageHelper.startPage(unQualifiedQualityDto.getPage(), unQualifiedQualityDto.getLimit());
        PageInfo<UnQualifiedQualityVo> pageInfo = new PageInfo<>(reportsService.unQualifiedQualityList(unQualifiedQualityDto));
        session.setAttribute("unQualifiedQualityList", pageInfo.getList());
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 质检不合格未上架看板
     */
    @GetMapping("/qualifiedQualityList")
    @ResponseBody
    public ApiResponse qualifiedQualityList(HttpSession session, @RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int limit,
                                            UnQualifiedQualityDto unQualifiedQualityDto) {
        PageHelper.startPage(page, limit);
        PageInfo<UnQualifiedQualityVo> pageInfo = new PageInfo<>(reportsService.qualifiedQualityList(unQualifiedQualityDto));
        session.setAttribute("qualifiedQualityList", pageInfo.getList());
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 收获日对账
     */
    @GetMapping("/reconciliationList")
    @ResponseBody
    public ApiResponse reconciliationList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                          HttpSession session, @RequestParam(required = false) String startTime,
                                          @RequestParam(required = false) String endTime) {

        PageHelper.startPage(page, limit);
        PageInfo<ReceivingReconciliationVo> pageInfo = new PageInfo<>(reportsService.reconciliationList(startTime, endTime));
        session.setAttribute("reconciliationList", reportsService.reconciliationList(startTime, endTime));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * iqc 履历日对账统计表
     */
    @GetMapping("/iqcHistoryReconciliationList")
    @ResponseBody
    public ApiResponse iqcHistoryReconciliationList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                                    HttpSession session, @RequestParam(required = false) String startTime,
                                                    @RequestParam(required = false) String endTime) {

        PageHelper.startPage(page, limit);
        PageInfo<IqcHistoryReconciliationVo> pageInfo = new PageInfo<>(reportsService.iqcHistoryReconciliationList(startTime, endTime));
        session.setAttribute("iqcHistoryReconciliationList", reportsService.iqcHistoryReconciliationList(startTime, endTime));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * IQC来料日报表
     */
    @GetMapping("/suppliedMaterialsList")
    @ResponseBody
    public ApiResponse suppliedMaterialsList(String whseid, String users,
                                             HttpServletResponse response,
                                             @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                             HttpSession session,
                                             SuppliedMaterialsDailyVo suppliedMaterialsDailyVo) {

        SaveWhseIdToCookie.setWhseIdSaveToCookie(whseid, users, response, session);
        PageHelper.startPage(page, limit);
        PageInfo<SuppliedMaterialsDailyVo> pageInfo = new PageInfo<>(reportsService.suppliedMaterialsList(suppliedMaterialsDailyVo));
        session.setAttribute("suppliedMaterialsList", reportsService.suppliedMaterialsList(suppliedMaterialsDailyVo));
        session.setAttribute("modelsMgtList", modelsMgtService.list(null));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 货主下拉框
     */
    @GetMapping("/getShipper")
    @ResponseBody
    public ApiResponse getShipper() {
        return ApiResponse.ok(iqcDetectionHistoryService.getShipperList());
    }

    /**
     * 机型下拉框
     */
    @GetMapping("/getModelKey")
    @ResponseBody
    public ApiResponse getModelKey(String shipper) {
        return ApiResponse.ok(iqcDetectionHistoryService.getModelKeyList(shipper));
    }

    /**
     * 判定方式下拉框
     */
    @GetMapping("/getDecideWay")
    @ResponseBody
    public ApiResponse getDecideWay() {
        return ApiResponse.ok(iqcDetectionHistoryService.getDecideWay());
    }

    /**
     * IQC来料周报表
     */
    @GetMapping("/suppliedMaterialsCheckWeekReportList")
    public String suppliedMaterialsCheckWeekReportList(String whseid, String users,
                                                       HttpServletResponse response, HttpServletRequest request, HttpSession session,
                                                       String startTime, String endTime, String modelKey,
                                                       String shipper) throws ParseException {


        SaveWhseIdToCookie.setWhseIdSaveToCookie(whseid, users, response, session);
        //机型
        List<ModelsMgt> modelsMgtList = modelsMgtService.list(null);
        request.setAttribute("modelsMgtList", modelsMgtList);
        Map<String, Object> map = reportsService.suppliedMaterialsCheckWeekReportList(startTime, endTime, modelKey, shipper);
        List<Integer> weekList = (List<Integer>) map.get("weekList");
        List<SuppliedMaterialsCheckWeekReportVo> suppliedMaterialsCheckWeekReportList = (List<SuppliedMaterialsCheckWeekReportVo>) map.get("suppliedMaterialsCheckWeekReportList");
        request.setAttribute("startTime", startTime);
        request.setAttribute("endTime", endTime);
        request.setAttribute("modelKey", modelKey);
        request.setAttribute("selectShipper", shipper);
        session.setAttribute("suppliedMaterialsCheckWeekReportList", suppliedMaterialsCheckWeekReportList);
        session.setAttribute("weekList", weekList);
        return "/report/iqc/inspectionWeekReport";
    }

    /**
     * IQC来料周报表(按照物料大类)
     */
    @GetMapping("/suppliedMaterialsCheckWeekByUDF8ReportList")
    public String suppliedMaterialsCheckWeekByUDF8ReportList(String whseid, String users,
                                                             HttpServletResponse response,
                                                             HttpServletRequest request, HttpSession session,
                                                             String startTime, String endTime, String modelKey,
                                                             String shipper) throws ParseException {


        SaveWhseIdToCookie.setWhseIdSaveToCookie(whseid, users, response, session);
        //机型
        List<SuppliedMaterialsCheckWeekReportVo> suppliedMaterialsCheckWeekReportList =
                null;
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            List<ModelsMgt> modelsMgtList = modelsMgtService.list(null);
            request.setAttribute("modelsMgtList", modelsMgtList);
            Map<String, Object> map = reportsService.suppliedMaterialsCheckWeekByUDF8ReportList(startTime, endTime, modelKey, shipper);
            List<Integer> weekList = (List<Integer>) map.get("weekList");
            List<String> udf8List = (List<String>) map.get("udf8List");
            List<SuppliedMaterialsCheckWeekReportVo> sumList = (List<SuppliedMaterialsCheckWeekReportVo>) map.get("sumList");
            suppliedMaterialsCheckWeekReportList = (List<SuppliedMaterialsCheckWeekReportVo>) map.get("suppliedMaterialsCheckWeekReportList");
            request.setAttribute("startTime", startTime);
            request.setAttribute("endTime", endTime);
            request.setAttribute("modelKey", modelKey);
            session.setAttribute("weekList", weekList);
            session.setAttribute("sumList", sumList);
            session.setAttribute("udf8List", udf8List);
            request.setAttribute("selectShipper", shipper);
        }
        session.setAttribute("suppliedMaterialsCheckWeekReportList", suppliedMaterialsCheckWeekReportList);
        return "/report/iqc/inspectionWeekByUdf8Report";
    }

    /**
     * IQC来料月报表
     */
    @GetMapping("/suppliedMaterialsCheckMonthReport")
    public String suppliedMaterialsCheckMonthReport(String whseid, String users,
                                                    HttpServletResponse response, HttpServletRequest request, HttpSession session,
                                                    String startTime, String endTime,
                                                    String modelKey, String shipper) throws ParseException {
        SaveWhseIdToCookie.setWhseIdSaveToCookie(whseid, users, response, session);

        //机型
        List<ModelsMgt> modelsMgtList = iqcDetectionHistoryService.getModelKeyList(shipper);
        List<SuppliedMaterialsCheckMonthReportVo> suppliedMaterialsCheckMonthReportVo = null;
        request.setAttribute("modelsMgtList", modelsMgtList);
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {

            Map<String, Object> map = reportsService.suppliedMaterialsCheckMonthReport(startTime, endTime, modelKey, shipper);
            List<Integer> monthList = (List<Integer>) map.get("monthList");
            suppliedMaterialsCheckMonthReportVo = (List<SuppliedMaterialsCheckMonthReportVo>) map.get("suppliedMaterialsCheckMonthReportVoList");
            Map<String, Object> sumMap = (Map<String, Object>) map.get("sumMap");
            request.setAttribute("startTime", startTime);
            request.setAttribute("endTime", endTime);
            request.setAttribute("modelsMgtList", modelsMgtList);
            session.setAttribute("sumMap", sumMap);
            request.setAttribute("modelKey", modelKey);
            request.setAttribute("selectShipper", shipper);
            session.setAttribute("monthList", monthList);
        }
        session.setAttribute("suppliedMaterialsCheckMonthReportVo", suppliedMaterialsCheckMonthReportVo);
        return "/report/iqc/inspectionMonthReport";
    }

    /**
     * 物料大类柱状图
     */
    @GetMapping("/toInspectionWeekByUdf8Chart")
    public String toInspectionWeekByUdf8Chart(String type, HttpServletRequest request) {
        request.setAttribute("type", type);
        return "/report/iqc/chart/inspectionWeekByUdf8Chart";
    }

    /**
     * 物料大类饼状图
     */
    @GetMapping("/toInspectionWeekByUdf8Chart2")
    public String toInspectionWeekByUdf8Chart2(String startTime, String endTime, HttpServletRequest request) throws ParseException {
        String year = startTime.split("-")[0];
        int week = reportsService.transformDateToWeek(startTime);
        request.setAttribute("year", year);
        request.setAttribute("week", week);
        request.setAttribute("startTime", startTime);
        request.setAttribute("endTime", endTime);
        return "/report/iqc/chart/inspectionWeekByUdf8Chart2";
    }

    /**
     * IQC来料检验十差供应商表Vo
     */
    @GetMapping("/suppliedMaterialsBadTop10Report")
    @ResponseBody
    public ApiResponse suppliedMaterialsBadTop10Report(HttpServletRequest request, HttpSession session,
                                                       String time) {


        List<SuppliedMaterialsBadTop10Vo> suppliedMaterialsBadTop10VoList = reportsService.suppliedMaterialsBadTop10List("ng", time);
        session.setAttribute("suppliedMaterialsBadTop10List", suppliedMaterialsBadTop10VoList);
        return ApiResponse.ok(suppliedMaterialsBadTop10VoList);
    }

    /**
     * IQC来料检验十佳供应商表Vo
     */
    @GetMapping("/suppliedMaterialsGoodTop10Report")
    @ResponseBody
    public ApiResponse suppliedMaterialsGoodTop10Report(HttpServletRequest request, HttpSession session,
                                                        String time) {


        List<SuppliedMaterialsBadTop10Vo> suppliedMaterialsGoodTop10VoList = reportsService.suppliedMaterialsBadTop10List("ok", time);
        session.setAttribute("suppliedMaterialsGoodTop10List", suppliedMaterialsGoodTop10VoList);
        return ApiResponse.ok(suppliedMaterialsGoodTop10VoList);
    }

    /**
     * 十佳供应商修改
     */
    @PostMapping("/updateTop10")
    @ResponseBody
    public void updateTop10(@RequestBody Object jsonStr) {
        ObjectMapper mapper = new ObjectMapper();
        SuppliedMaterialsBadTop10Vo suppliedMaterialsBadTop10Vo = mapper.convertValue(jsonStr, SuppliedMaterialsBadTop10Vo.class);
        reportsService.updateTop10(suppliedMaterialsBadTop10Vo);
    }


    /**
     * 超期物料重检表
     */
    @GetMapping("/getOverdueMaterialList")
    @ResponseBody
    public ApiResponse getOverdueMaterialList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                         OverdueMaterialDto overdueMaterialDto) {

        PageHelper.startPage(page, limit);
        PageInfo<OverdueMaterialVo> pageInfo = new PageInfo<>(reportsService.getOverdueMaterialList(overdueMaterialDto));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }
    /**
     * 不良品及验退超3天明细表
     */
    @GetMapping("/getBadSkuOverThreeDays")
    @ResponseBody
    public ApiResponse getBadSkuOverThreeDays(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                              BadSkuOverThreeDaysDto badSkuOverThreeDaysDto) {

        PageHelper.startPage(page, limit);
        PageInfo<BadSkuThreeDaysVo> pageInfo = new PageInfo<>(reportsService.getBadSkuOverThreeDays(badSkuOverThreeDaysDto));
        return ApiResponse.ok(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     *  8D文件上传后,将文件名写入履历表
     * */
    @PostMapping("/updateHistory")
    @ResponseBody
    public void updateHistory(@RequestBody Object jsonStr){
        ObjectMapper mapper = new ObjectMapper();
        SuppliedMaterialsDailyVo suppliedMaterialsDailyVo = mapper.convertValue(jsonStr, SuppliedMaterialsDailyVo.class);
        iqcDetectionHistoryService.updateHistory(suppliedMaterialsDailyVo);
    }
}
